package com.cloudpark.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cloudpark.common.util.Constants;
import com.cloudpark.common.util.MessageUtil;
import com.cloudpark.service.RegionService;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/region")
public class RegionController extends BaseController {

	private static final Logger log = Logger.getLogger(RegionController.class);
	
	@Autowired
    private RegionService regionService;
	
    @RequestMapping(value = "/queryCityOptions", method = RequestMethod.POST)
    @ResponseBody
    public String queryCityOptions(HttpServletRequest request,
            HttpServletResponse response) {
    	String result = "";
    	Map<String, Object> map = Maps.newHashMap();
        try {
        	String options = regionService.queryCityOptions();
        	map.put("code", Constants.OK);
        	map.put("options", options);
        	result = JSON.toJSONString(map);
        } catch (Exception e) {
            log.error("query city options error :", e);
            result = MessageUtil.error("服务器忙!");
        }
        return result;
    }
    
    @RequestMapping(value = "/queryAreaOptions", method = RequestMethod.POST)
    @ResponseBody
    public String queryAreaOptions(HttpServletRequest request,
            HttpServletResponse response) {
    	String result = "";
    	Map<String, Object> map = Maps.newHashMap();
        try {
        	String city = request.getParameter("city");
        	String options = regionService.queryAreaOptions(city);
        	map.put("code", Constants.OK);
        	map.put("options", options);
        	result = JSON.toJSONString(map);
        } catch (Exception e) {
            log.error("query area options error :", e);
            result = MessageUtil.error("服务器忙!");
        }
        return result;
    }
}
